package net.hvacapp.cmvrfforandroid.ui.product;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.constant.Constant;
import net.hvacapp.cmvrfforandroid.db.dao.imp.CacheDao;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetProductDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.GuidData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangnorman on 16/1/13.
 */
@EActivity(R.layout.activity_select_xs)
public class ProductXsActivity extends BaseActivity {

    @ViewById(R.id.lv_templete)
    ListView mTempleteLv;

    private QuickAdapter<GuidData> mQuickAdapter;

    private String mTag;
    private String mXl;




    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initViews();
        initEvent();
    }

    /***
     * 初始化视图
     */
    private void initViews() {
//        mActionView.setActionInfo(ProjectManager.getInstance().getTotal(), ProjectManager.getInstance().getCurrent());
    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "型式列表", null);
        if (mQuickAdapter == null){
            mQuickAdapter = new QuickAdapter<GuidData>(context, R.layout.one_line_cell_item) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final GuidData item) {
                    helper.setVisible(R.id.iv_selected_mark, true);
                    helper.setImageResource(R.id.iv_selected_mark, R.drawable.user_selection_hint);
                    helper.setText(R.id.tv_title, item.getName());
                }
            };
        }

        mTempleteLv.setAdapter(mQuickAdapter);
    }


    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        mTag = intent.getStringExtra("tag");
        mXl = intent.getStringExtra("xl");
        loadCache();
        loadProductXs(mTag);
    }

    private void loadCache() {
        CacheDao dao = new CacheDao(ProductXsActivity.this);

        GetProductDataResp obj = dao.getResponse(HttpConstant.HTTP_GET_PROJECT_SERIES_XS + mTag + mXl, AccountManager.getInstance().getToken(), GetProductDataResp.class);
        if (obj != null && obj.getGetProductXlsResult() != null
                && obj.getGetProductXlsResult().size() > 0) {
            // 更新列表
            mQuickAdapter.clear();
            mQuickAdapter.addAll(obj.getGetProductXlsResult());
        }
    }


    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }

    private void initEvent() {

        mTempleteLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GuidData data = (GuidData)parent.getItemAtPosition(position);
                Intent intent = new Intent(ProductXsActivity.this, AnnotationUtil.get(ProductListActivity.class));
                intent.putExtra("tag", mTag);
                intent.putExtra("xs", data.getGuidId());
                startActivity(intent);
            }
        });

    }


    /**
     *  获得可选得产品系列列表
     *
     *  @param tag
     */
    private void loadProductXs(final String tag) {
        Map<String, Object> param = new HashMap<>();
        param.put("tag", tag);
        param.put("xl", mXl);
        param.put("ac", 1); //0 仅包括未上市  1 仅包括已上市 2 已上市未上市都包括

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_PROJECT_SERIES_XS, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetProductDataResp>() {
            @Override
            public void onSuccess(GetProductDataResp obj) {
                // 成功
                if (obj.getGetProductXssResult() != null && obj.getGetProductXssResult().size() > 0) {
                    boolean needRefresh = true;
                    //写入缓存
                    CacheDao dao = new CacheDao(ProductXsActivity.this);
                    needRefresh = dao.setResponse(HttpConstant.HTTP_GET_PROJECT_SERIES_XS + mTag + mXl, AccountManager.getInstance().getToken(), Constant.CACHE_EXPIRED_TIME, obj);
                    // 更新列表
                    if (needRefresh) {
                        mQuickAdapter.clear();
                        mQuickAdapter.addAll(obj.getGetProductXssResult());
                    }
                }
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取系列失败，请重试！");
            }
        });
    }
}
